package com.code.generate;

import java.util.Locale;

import com.code.generate.enums.FieldType;

public class CommonUtils {
	public static String getProperty(String fieldName) {
		String property = "";
		if (fieldName.length() > 0) {
			String[] t = fieldName.split("_");
			property = t[0].toLowerCase();
			for (int i = 1; i < t.length; i++) {
				if (t[i].length() > 1) {
					property += t[i].substring(0, 1).toUpperCase() + t[i].substring(1).toLowerCase();
				} else {
					property += t[i].toUpperCase();
				}
			}
		}
		return property;
	}

	public static String getFieldType(String colType, String columnName) {
		String fieldType = null;
		if ("CHAR".equals(colType) && columnName.toUpperCase().startsWith("IS_")) {
			fieldType = "Boolean";
		} else {
			fieldType = getFieldType(colType);
		}
		return fieldType;
	}

	public static String getFieldType(String colType) {
		String fieldType = null;
		if (colType != null) {
			colType = colType.trim().toUpperCase();
		}
		if (isString(colType)) {
			fieldType = "String";
		} else if ("DECIMAL".equals(colType)) {
			fieldType = "BigDecimal";
		} else if ("DOUBLE".equals(colType)) {
			fieldType = "Double";
		} else if (isInteger(colType)) {
			fieldType = FieldType.INTEGER.toString();
		} else if ("TIMESTMP".equals(colType)) {
			fieldType = "Timestamp";
		} else if ("TIME".equals(colType)) {
			fieldType = "Time";
		} else if ("DATE".equals(colType) || "DATETIME".equals(colType)) {
			fieldType = FieldType.DATE.toString();
		} else if ("BLOB".equals(colType)) {
			fieldType = "BLOB";
		} else {
			fieldType = "String";
		}
		return fieldType;
	}

	private static boolean isString(String colType) {
		return colType == null || colType.length() == 0 || "VARCHAR".equals(colType) || "VARCHAR2".equals(colType) || "CLOB".equals(colType) || "CHAR".equals(colType) || "LONGVAR".equals(colType);
	}

	private static boolean isInteger(String colType) {
		return "INTEGER".equals(colType)
				|| "SMALLINT".equals(colType)
				|| "INT".equals(colType);
	}

	/**
	 * 从"@{表名}"这种类型字符串中获取变量名，即"表名"
	 * 
	 * @description: <方法描述>
	 * @param key
	 * @return 变量名
	 */
	public static String getVariable(String key) {
		return key.substring(2, key.length() - 1);
	}

	/**
	 * 将字符串第一个字母转为小写
	 * 
	 * @description: <方法描述>
	 * @param str
	 * @return
	 */
	public static String changeFirstLower(String str) {
		return str.substring(0, 1).toLowerCase(Locale.ENGLISH) + str.substring(1, str.length());
	}

	/**
	 * 将字符串第一个字母转为大写
	 * 
	 * @description: <方法描述>
	 * @param str
	 * @return
	 */
	public static String changeFirstUpper(String str) {
		return str.substring(0, 1).toUpperCase(Locale.ENGLISH) + str.substring(1, str.length());
	}

	public static String changeSplit(String str) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (sb.length() > 0 && c >= 'A' && c <= 'Z')
				sb.append("-");
			sb.append(String.valueOf(c).toLowerCase(Locale.ENGLISH));
		}
		return sb.toString();
	}
}
